package day01;

import java.util.Objects;

public class Animal {
    public static void main(String[] args) {
        Dog dog =new Dog("黑子",18);
        Dog dog1 =new Dog("黑子",19);
        System.out.println( dog.equals(dog1));
    }
}


class Dog {
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        System.out.println(this == obj);
        // 检查是否为同一个对象的引用
        if (this == obj) return true;
        // 检查是否为null以及是否为正确的类型
        if (obj == null || getClass() != obj.getClass()) return false;
        // 类型转换
        Dog dog = (Dog) obj;
        // 比较关键字段
        return age == dog.age && (name != null ? name.equals(dog.name) : dog.name == null);
    }


    @Override
    public int hashCode() {
        // 使用Objects类的hash方法可以确保null值被正确处理并产生一致的hashCode值
        return Objects.hash(name, age);
    }
}



